﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace XYX.Http.Mvc
{

    /// <summary>
    /// 基类控制器
    /// </summary>
    public class BaseController
    {
        /// <summary>
        /// 路由
        /// </summary>
        public RouteData RouteData { get; set; }
        /// <summary>
        /// 控制器类型
        /// </summary>
        public Type ControllerType { get; set; }

        /// <summary>
        /// 上下文
        /// </summary>

        public HttpListenerContext HttpListenerContext { get; set; }

        /// <summary>
        /// 返回Json
        /// </summary>
        /// <param name="data"></param>
        /// <param name="jsonBehavior"></param>
        /// <returns></returns>

        protected virtual JsonResult Json(object data, JsonRequestBehavior jsonBehavior)
        {
            data = data ?? throw new ArgumentException(nameof(data));
            JsonResult result = new JsonResult()
            {
                Data = data,
                JsonRequestBehavior = jsonBehavior
            };
            return result;
        }
        /// <summary>
        /// 返回Json 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected virtual JsonResult Json(object data)
        {
            JsonResult result = new JsonResult()
            {
                Data = data,
            };
            return result;
        }
        /// <summary>
        /// 返回字符串
        /// </summary>
        /// <param name="content"></param>
        /// <param name="contentType"></param>
        /// <param name="contentEncoding"></param>
        /// <returns></returns>
        protected virtual ContentResult Content(string content, string contentType, Encoding contentEncoding)
        {
            ContentResult result = new ContentResult()
            {
                Content = content,
                ContentType = contentType,
                ContentEncoding = contentEncoding
            };
            return result;
        }
        /// <summary>
        /// 返回text/plain为空
        /// </summary>
        /// <param name="object"></param>
        /// <returns></returns>
        protected virtual EmtiyResult Emtiy(object @object)
        {
            EmtiyResult result = new EmtiyResult() { };
            return result;

        }
        /// <summary>
        /// 下载
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        protected virtual DownFileResult DownFile(string path)
        {
            return new DownFileResult()
            {
                FilePath = path,
            };

        }
        /// <summary>
        /// 视图
        /// </summary>
        /// <param name="viewPath"></param>
        /// <returns></returns>
        protected virtual ViewResult View(string viewPath = "")
        {
            return new ViewResult() { ViewPath = viewPath };

        }

        /// <summary>
        /// 执行之前
        /// </summary>
        /// <param name="routeData"></param>
        /// <param name="controllerType"></param>
        public virtual void OnActionExecuting(RouteData routeData, Type controllerType)
        {
            //ToDO

        }
        /// <summary>
        /// 执行之后
        /// </summary>
        /// <param name="routeData"></param>
        /// <param name="controllerType"></param>
        public virtual void OnActionExecuted(RouteData routeData, Type controllerType)
        {
            //ToDO
        }

    }
}
